class Product < ActiveRecord::Base
  belongs_to :vendor

  #得到所有通过审批的产品
  def self.findProductsOfPass name,vendor_name,pageBegin,pageEnd
    conditions = 'name like ? and auditing_status = "通过" and exists (select 1 from vendors where company_name like ?)'
    find(:all,:conditions=>[conditions,'%'+name+'%','%'+vendor_name+'%'],:offset=>pageBegin.to_i,:limit =>pageEnd.to_i)
    #"{totalProperty:"+allProducts.length.to_s+",root:"+limitedProducts.to_json(:only => [:id,:name,:brand,:model,:specification,:manufacturer,:unit_price,:auditing_status,:reject_reason])+"}"
    #"{totalProperty:"+allProducts.length.to_s+",root:"+limitedProducts.to_json(:include=>[:vendor])+"}"
  end

  #得到登录供应商的产品
  def self.findProductsOfVendor vendorId,name,state,pageBegin,pageEnd
    find(:all,:conditions=>['vendor_id = ? and name like ? and auditing_status like ?',vendorId,'%'+name+'%','%'+state+'%'],:offset=>pageBegin.to_i,:limit =>pageEnd.to_i)
  end

  #得到正在等待审批的产品
  def self.findProductsForAuditor name ,vendor_name,pageBegin,pageEnd
    conditions = 'name like ? and auditing_status = "未审批" and exists (select 1 from vendors where company_name like ?)'
    find(:all,:conditions=>[conditions,'%'+name+'%','%'+vendor_name+'%'],:offset=>pageBegin.to_i,:limit =>pageEnd.to_i)
  end
end
